<?php
declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\facade\Db;


/**
 * 用户列表
 * @mixin \think\Model
 */
class Member extends Model
{
    /**
     * 获取用户列表
     * @return [type] [description]
     */
    public function lists()
    {
        $result = self::alias('m')
            ->join('member_detail d', 'd.member_id = m.id','LEFT')
            ->field('m.id,m.name,m.avatar,m.sex,d.advantage,m.participation_time,education_level')
            ->where("name != ''")
            ->where("sex != ''")
            ->where("education_level != ''")
            ->where('default_identity',0)
            ->order('m.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            if (!$value['name']) {
                $result['data'][$key]['name'] = "牛人";
            }
        	// 求职意向
        	$result['data'][$key]['objective'] = $this->objectives($value['id']);
            // 经验
            $result['data'][$key]['participation_num'] = $value['participation_time'] ? diffDate($value['participation_time'],time()) : 0;
            // 性别
            $result['data'][$key]['sex'] = sextype($value['sex']);
            $result['data'][$key]['education_level'] = education_level($value['education_level']);
        }

        return ['list'=>$result];
    }

    /**
     * 求职者列表
     * @return [type] [description]
     */
    public function favorites($company_id)
    {
        $result = Db::table('ea_favorites f')
            ->join('ea_member m','f.member_id = m.id')
            ->join('ea_member_detail d', 'd.member_id = m.id','LEFT')
            ->field('m.id,m.name,m.avatar,m.sex,d.advantage,m.participation_time,education_level,f.add_time')
            ->where('f.type',2)
            ->where('f.company_id',$company_id)
            ->order('m.id desc')
            ->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            // 求职意向
            $result['data'][$key]['objective'] = $this->objectives($value['id']);
            // 经验
            $result['data'][$key]['participation_num'] = diffDate($value['participation_time'],time());
            // 性别
            $result['data'][$key]['sex'] = sextype($value['sex']);
            $result['data'][$key]['education_level'] = education_level($value['education_level']);
        }

        return ['list'=>$result];
    }

    /**
     * 求职意向
     * @return [type] [description]
     */
    public function objectives($id)
    {
        $objective = Db::table('ea_objective')->where('member_id',$id)->field('nature,area_no,name,expect')->order('id desc')->select()->toArray();

        foreach ($objective as $key => $value) {
            $objective[$key]['area_no_text'] = areaNoText($value['area_no']);
        }

        return $objective;
    }

    /**
     * 教育经历
     * @return [type] [description]
     */
    public function educationa($id)
    {
        $educationa = Db::table('ea_educationa')->where('member_id',$id)->field('id,name,major_name,education,starttime,endtime,describe,describe')->select()->toArray();


        foreach ($educationa as $key => $value) {
            $educationa[$key]['starttime'] = date("Y-m",$value['starttime']);
            $educationa[$key]['endtime'] = date("Y-m",$value['endtime']);
        }

        return $educationa;
    }

    /**
     * 工作经历
     * @return [type] [description]
     */
    public function experience($id)
    {
        $experience = Db::table('ea_experience')->where('member_id',$id)->field('id,name,starttime,endtime,positionname,area_no,describe')->select()->toArray();

        foreach ($experience as $key => $value) {
            $experience[$key]['starttime'] = date("Y-m",$value['starttime']);
            $experience[$key]['endtime'] = date("Y-m",$value['endtime']);
            $experience[$key]['area_no_text'] = areaNoText($value['area_no']);
        }

        return $experience;
    }

    /**
     * 搜索牛人
     * @return [type] [description]
     */
    public function searchlists($params)
    {
        $where = [];
        $wap = '';

        $model = self::alias('m')
            ->join('member_detail d', 'd.member_id = m.id','LEFT')
            ->join('ea_educationa e', 'e.member_id = m.id','LEFT')
            ->join('ea_objective o', 'o.member_id = m.id','LEFT')
            ->group('m.id')
            ->where('birthday > 0')
            ->where('participation_time > 0')
            ->field('m.id,m.name,m.avatar,m.phone,m.sex,d.advantage,m.participation_time,education_level,birthday,working_status')
            ->order('m.id desc');

        $model = $model->where('default_identity',0);
        $model = $model->where("m.name != ''");

        if (isset($params['sex']) && $params['sex']) {
            if ($params['sex'] == '男') {
                $params['sex'] = 1;
            } else {
                $params['sex'] = 2;
            }
            $model = $model->where('sex',$params['sex']);
        }
        if (isset($params['working_status']) && $params['working_status']) {
            if ($params['working_status'] == '离职-随时到岗') $params['working_status'] = 0;
            if ($params['working_status'] == '在职-月内到岗') $params['working_status'] = 1;
            if ($params['working_status'] == '在职-考虑机会') $params['working_status'] = 2;
            if ($params['working_status'] == '在职-暂不考虑') $params['working_status'] = 3;

            $model = $model->where('working_status',$params['working_status']);
        }
        // 年龄检索
        if (isset($params['start_age']) && $params['start_age'] && isset($params['end_age']) && $params['end_age']) {
            $model = $model->whereTime('birthday','between',[strtotime("-".$params['end_age']." year"),strtotime("-".$params['start_age']." year")]);
        } else {
            if (isset($params['start_age']) && $params['start_age']) {
                $model = $model->whereTime('birthday','<',date("Y-m-d",strtotime("-".$params['start_age']." year")));
            }
            if (isset($params['end_age']) && $params['end_age']) {
                $model = $model->whereTime('birthday','>',strtotime("-".$params['end_age']." year"));
            }
        }
        // 工作年限检索
        if (isset($params['start_work']) && $params['start_work'] && isset($params['end_work']) && $params['end_work']) {
            $model = $model->whereTime('participation_time','between',[strtotime("-".$params['end_work']." year"),strtotime("-".$params['start_work']." year")]);
        } else {
            if (isset($params['start_work']) && $params['start_work']) {
                $model = $model->whereTime('participation_time','<',date("Y-m-d",strtotime("-".$params['start_work']." year")));
            }
            if (isset($params['end_work']) && $params['end_work']) {
                $model = $model->whereTime('participation_time','>',strtotime("-".$params['end_work']." year"));
            }
        }
        // 学历
        if (isset($params['xueli']) && $params['xueli']) {
            if ($params['xueli'] == '初中') $params['xueli'] = 1;
            if ($params['xueli'] == '高中') $params['xueli'] = 2;
            if ($params['xueli'] == '中技') $params['xueli'] = 3;
            if ($params['xueli'] == '中专') $params['xueli'] = 4;
            if ($params['xueli'] == '大专') $params['xueli'] = 5;
            if ($params['xueli'] == '本科') $params['xueli'] = 6;
            if ($params['xueli'] == '硕士') $params['xueli'] = 7;
            if ($params['xueli'] == '博士') $params['xueli'] = 8;
            if ($params['xueli'] == '博后') $params['xueli'] = 9;

            $model = $model->where('education_level',$params['xueli']);
        }
        if (isset($params['search']) && $params['search']) {
            $search = $params['search'];
            $model = $model->where('e.name|e.describe|o.name','like',"%$search%");
        }

        $result = $model->paginate(10);

        $result = $result->toArray();

        foreach ($result['data'] as $key => $value) {
            // 求职意向
            $objective = $this->objectives($value['id']);
            if ($objective) {
                $result['data'][$key]['objective'] = $objective[0];
            } else {
                $result['data'][$key]['objective'] = [];
            }

            // 教育经历
            $educationa = $this->educationa($value['id']);
            if ($educationa) {
                $result['data'][$key]['educationa'] = $educationa[0];
            } else {
                $result['data'][$key]['educationa'] = [];
            }

            if (!$value['name']) {
                $result['data'][$key]['name'] = "牛人";
            }
            // 经验
            if ($value['participation_time']) {
                $result['data'][$key]['participation_num'] = diffDate($value['participation_time'],time());
            } else {
                $result['data'][$key]['participation_num'] = '';
            }

            // 年龄
            if($value['birthday']) {
                $result['data'][$key]['birthday_num'] = diffDate($value['birthday'],time());
            } else {
                $result['data'][$key]['birthday_num'] = '';
            }

            // 性别
            $result['data'][$key]['sex'] = sextype($value['sex']);
            $result['data'][$key]['working_status'] = workingstatus($value['working_status']);
            $result['data'][$key]['education_level'] = education_level($value['education_level']);

        }

        return ['list'=>$result];
    }

    /**
     * 获取用户信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getinfo($id){
        $data = self::alias('m')
            ->join('member_detail d', 'd.member_id = m.id','LEFT')
            ->field('m.id,m.name,m.avatar,m.sex,d.advantage,m.participation_time,education_level,birthday,working_status')
            ->where('m.id',$id)
            ->find();

        // 求职意向
        $objective = $this->objectives($data['id']);
        if ($objective) {
            $data['objective'] = $objective;
        } else {
            $data['objective'] = [];
        }

        // 教育经历
        $educationa = $this->educationa($data['id']);
        if ($educationa) {
            $data['educationa'] = $educationa;
        } else {
            $data['educationa'] = [];
        }

        $experience = $this->experience($data['id']);
        if ($experience) {
            $data['experience'] = $experience;
        } else {
            $data['experience'] = [];
        }

        // 经验
        if ($data['participation_time']) {
            $data['participation_num'] = diffDate($data['participation_time'],time());
        } else {
            $data['participation_num'] = '';
        }
        
        // 年龄
        if ($data['birthday']) {
            $data['birthday_num'] = diffDate($data['birthday'],time());
        } else {
            $data['birthday_num'] = '';
        }

        // 性别
        $data['sex'] = sextype($data['sex']);
        $data['education_level'] = education_level($data['education_level']);

        

        return $data;
    }
}
